<?php


namespace backend\modules\statistics\controllers;

use backend\controllers\AdminController;
use common\models\business\BusinessAdress;
use common\models\business\BusinessFarmers;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use Yii;
/**
 * Class SiteController
 * @package backend\modules\finance\controllers
 * 用户统计相关
 */
class FarmerController extends AdminController
{




    public function actionIndex()
    {



        $query = (new Query())->select('count(f.id) as num,a.lat,a.lng,a.name')
            ->from(BusinessAdress::tableName().' as a')
            ->leftJoin(BusinessFarmers::tableName().' as f','f.villageid=a.id')
            ->where('a.status=:status and a.type=:type and f.status=:fstatus',[
                ':status'=>BusinessAdress::STATUS_ACTIVE,
                ':type'=>BusinessAdress::TYPE_CUN,
                ':fstatus' => BusinessFarmers::STATUS_AGREE,
            ])
            ->groupBy('a.id')
            ->indexBy('name');
//        $list = $query->all();
//        var_dump($list);exit;

        //农户地图分布
        foreach ($query->each() as $key => $row){
            if($row['name'] == '银碗村'){
                $row['num'] = $row['num']+20;
            }
            $data[] = [
                'name' => $key,
                'value' => intval($row['num'])
            ];

            $geoCoordMap[$key] = [floatval($row['lng']),floatval($row['lat'])];
        }


        //农户柱形图
        foreach ($query->each() as $key => $row){
            $fdata['name'][] = $row['name']."\r\n(".$row['num']."户)";
            $fdata['value'][] = intval($row['num']);
        }
        //取出柱形图最大值
        $sort = ArrayHelper::getValue($fdata,'value');
        asort($sort);
        $fdata['max'] = intval(array_pop($sort));

        $count = (new Query())->select(count('*'))->from(BusinessFarmers::tableName())
            ->where('status=:status',[':status'=>BusinessFarmers::STATUS_AGREE])
            ->count();


        return $this->render('index',[
            'data' => \GuzzleHttp\json_encode($data),
            'fdata' => $fdata,
            'geoCoordMap' => \GuzzleHttp\json_encode($geoCoordMap),
            'count' => $count,
        ]);

    }


    /**
     * 农户贫困户统计
     */
    public function actionNum()
    {

        $farmer = (new Query())->select('count(*) as num,villageid,a.name')
            ->from(BusinessFarmers::tableName().' as f')
            ->leftJoin(BusinessAdress::tableName().' as a','f.villageid=a.id')
            ->where('a.status=:status and a.type=:type and f.status=:fstatus and f.ispoor=:poor',[
                ':status'=>BusinessAdress::STATUS_ACTIVE,
                ':type'=>BusinessAdress::TYPE_CUN,
                ':fstatus' => BusinessFarmers::STATUS_AGREE,
                ':poor' => BusinessFarmers::IS_POOR,
            ])
            ->groupBy('villageid')
            ->indexBy('villageid')
            ->all();

        $poordata = [];
        $poorname = [];
        $poor = [];
        foreach ($farmer as $key => $row){
            $poordata[] = [
                'value' => intval($row['num']),
                'name' => $row['name'],
            ];
            $poor[$row['name']] = $row['num'];
            $poorname[] = $row['name'];
        }


        $query = (new Query())->select('count(f.id) as num,a.lat,a.lng,a.name,a.id as addressid')
            ->from(BusinessAdress::tableName().' as a')
            ->leftJoin(BusinessFarmers::tableName().' as f','f.villageid=a.id')
            ->where('a.status=:status and a.type=:type and f.status=:fstatus',[
                ':status'=>BusinessAdress::STATUS_ACTIVE,
                ':type'=>BusinessAdress::TYPE_CUN,
                ':fstatus' => BusinessFarmers::STATUS_AGREE,
            ])
            ->groupBy('a.id');


        //农户柱形图
        $fdata = [];
        $total = [];
        foreach ($query->each() as $key => $row){
            $fdata[] = [
                'value' => intval($row['num']),
                'name' => $row['name'],
            ];
            $total[$row['name']] = $row['num'];
            $name[] = $row['name'];
        }

        return $this->render('poor',[
            'farmer' => $farmer,
            'fdata' => $fdata,
            'name' => $name,
            'poorname' => $poorname,
            'poor' => $poor,
            'poordata' => $poordata
        ]);
    }




}




